From ec0596916e9ae9e5432dc62e7fe914894ce2f025 Mon Sep 17 00:00:00 2001 From: Krinkle Date: Sun, 10 Jul 2011 19:10:51 +0000 Subject: [PATCH] Centralize QUnit customizations for MediaWiki: - Rename 'awesome.js' to 'defineTestCallback.js' (more descriptive) - Move all runner customizations into 'testrunner.js' - Move all customization files into /data - Fix bug in jquery.qunit.completenesstest that sometimes caused the element to be re-created if it was ran again from the console. - Centralize additional assertion helpers in testrunner.js --- .../jquery/jquery.qunit.completenessTest.js | 4 +- .../awesome.js => data/defineTestCallback.js} | 2 +- tests/qunit/data/testrunner.js | 96 +++++++++++++++++++ .../testwarm.inject.js} | 0 tests/qunit/index.html | 15 +-- .../jquery.qunit.completenessTest.config.js | 25 ----- .../resources/jquery/jquery.autoEllipsis.js | 21 ---- .../suites/resources/mediawiki/mediawiki.js | 2 +- 8 files changed, 103 insertions(+), 62 deletions(-) rename tests/qunit/{sample/awesome.js => data/defineTestCallback.js} (65%) create mode 100644 tests/qunit/data/testrunner.js rename tests/qunit/{testswarm.inject.js => data/testwarm.inject.js} (100%) delete mode 100644 tests/qunit/jquery.qunit.completenessTest.config.js diff --git a/resources/jquery/jquery.qunit.completenessTest.js b/resources/jquery/jquery.qunit.completenessTest.js index 7d4f346123..2244237cb4 100644 --- a/resources/jquery/jquery.qunit.completenessTest.js +++ b/resources/jquery/jquery.qunit.completenessTest.js @@ -67,7 +67,9 @@ var CompletenessTest = function ( masterVariable, ignoreFn ) { html += '
' + mw.html.escape(key); }); html += '

— CompletenessTest'; - return $( '
' ).css( style ).append( html ); + var $oldResult = $( '#qunit-completenesstest' ), + $result = $oldResult.length ? $oldResult : $( '
' ); + return $result.css( style ).html( html ); }; if ( $.isEmptyObject( that.missingTests ) ) { diff --git a/tests/qunit/sample/awesome.js b/tests/qunit/data/defineTestCallback.js similarity index 65% rename from tests/qunit/sample/awesome.js rename to tests/qunit/data/defineTestCallback.js index d7852e3dd2..6fcd45951f 100644 --- a/tests/qunit/sample/awesome.js +++ b/tests/qunit/data/defineTestCallback.js @@ -1,4 +1,4 @@ -window.mw.loader.testCallback = function(){ +window.mw.loader.testCallback = function() { start(); ok( true, 'Implementing a module, is the callback timed properly ?'); }; diff --git a/tests/qunit/data/testrunner.js b/tests/qunit/data/testrunner.js new file mode 100644 index 0000000000..446add1831 --- /dev/null +++ b/tests/qunit/data/testrunner.js @@ -0,0 +1,96 @@ +( function( $ ) { + +/** + * Add bogus to url to prevent IE crazy caching + * + * @param value {String} a relative path (eg. 'data/defineTestCallback.js' or 'data/test.php?foo=bar') + * @return {String} Such as 'data/defineTestCallback.js?131031765087663960' + */ +QUnit.fixurl = function(value) { + return value + (/\?/.test(value) ? "&" : "?") + new Date().getTime() + "" + parseInt(Math.random()*100000); +}; + +/** + * Load TestSwarm agent + */ +if ( QUnit.urlParams.swarmURL ) { + document.write(""); +} + +/** + * Load completenesstest + */ +if ( QUnit.urlParams.completenesstest ) { + + // Return true to ignore + var mwTestIgnore = function( val, tester, funcPath ) { + + // Don't record methods of the properties of constructors, + // to avoid getting into a loop (prototype.constructor.prototype..). + // Since we're therefor skipping any injection for + // "new mw.Foo()", manually set it to true here. + if ( val instanceof mw.Map ) { + tester.methodCallTracker['Map'] = true; + return true; + } + if ( val instanceof mw.Title ) { + tester.methodCallTracker['Title'] = true; + return true; + } + + // Don't record methods of the properties of a jQuery object + if ( val instanceof $ ) { + return true; + } + + return false; + }; + + var mwTester = new CompletenessTest( mw, mwTestIgnore ); +} + +/** + * Add-on assertion helpers + */ +// Define the add-ons +var addons = { + + // Expect boolean true + assertTrue: function( actual, message ) { + strictEqual( actual, true, message ); + }, + + // Expect boolean false + assertFalse: function( actual, message ) { + strictEqual( actual, false, message ); + }, + + // Expect numerical value less than X + lt: function( actual, expected, message ) { + QUnit.push( actual < expected, actual, 'less than ' + expected, message ); + }, + + // Expect numerical value less than or equal to X + ltOrEq: function( actual, expected, message ) { + QUnit.push( actual <= expected, actual, 'less than or equal to ' + expected, message ); + }, + + // Expect numerical value greater than X + gt: function( actual, expected, message ) { + QUnit.push( actual > expected, actual, 'greater than ' + expected, message ); + }, + + // Expect numerical value greater than or equal to X + gtOrEq: function( actual, expected, message ) { + QUnit.push( actual >= expected, actual, 'greater than or equal to ' + expected, message ); + }, + + // Backwards compatible with new verions of QUnit + equals: window.equal, + same: window.deepEqual +}; + +$.extend( QUnit, addons ); +$.extend( window, addons ); + +})( jQuery ); diff --git a/tests/qunit/testswarm.inject.js b/tests/qunit/data/testwarm.inject.js similarity index 100% rename from tests/qunit/testswarm.inject.js rename to tests/qunit/data/testwarm.inject.js diff --git a/tests/qunit/index.html b/tests/qunit/index.html index f27aa998f9..69c53368a7 100644 --- a/tests/qunit/index.html +++ b/tests/qunit/index.html @@ -51,6 +51,8 @@ + + @@ -67,19 +69,6 @@ - - - - - -

MediaWiki JavaScript Test Suite

diff --git a/tests/qunit/jquery.qunit.completenessTest.config.js b/tests/qunit/jquery.qunit.completenessTest.config.js deleted file mode 100644 index c03ddc9436..0000000000 --- a/tests/qunit/jquery.qunit.completenessTest.config.js +++ /dev/null @@ -1,25 +0,0 @@ -// Return true to ignore -var mwTestIgnore = function( val, tester, funcPath ) { - - // Don't record methods of the properties of constructors, - // to avoid getting into a loop (prototype.constructor.prototype..). - // Since we're therefor skipping any injection for - // "new mw.Foo()", manually set it to true here. - if ( val instanceof mw.Map ) { - tester.methodCallTracker['Map'] = true; - return true; - } - if ( val instanceof mw.Title ) { - tester.methodCallTracker['Title'] = true; - return true; - } - - // Don't record methods of the properties of a jQuery object - if ( val instanceof $ ) { - return true; - } - - return false; -}; - -var mwTester = new CompletenessTest( mw, mwTestIgnore ); diff --git a/tests/qunit/suites/resources/jquery/jquery.autoEllipsis.js b/tests/qunit/suites/resources/jquery/jquery.autoEllipsis.js index 62c94f0879..caf5a6f1f1 100644 --- a/tests/qunit/suites/resources/jquery/jquery.autoEllipsis.js +++ b/tests/qunit/suites/resources/jquery/jquery.autoEllipsis.js @@ -23,27 +23,6 @@ function findDivergenceIndex( a, b ) { test( 'Position right', function() { expect(4); - /** - * Extra QUnit assertions - * Needed in order to include the expected and actual values in the output. - * This way we end up with: - * "Expected: > 100, Result: 99" - * instead of: - * "Expected: true, Result: false" - */ - // Expect numerical value less than or equal to X - var ltOrEq = function( actual, expected, message ) { - QUnit.push( actual <= expected, actual, 'less than or equal to ' + expected, message ); - }; - // Expect numerical value greater than X - var gt = function( actual, expected, message ) { - QUnit.push( actual > expected, actual, 'greater than ' + expected, message ); - }; - // Expect numerical value greater than or equal to X - var gtOrEq = function( actual, expected, message ) { - QUnit.push( actual >= expected, actual, 'greater than or equal to ' + expected, message ); - }; - // We need this thing to be visible, so append it to the DOM var origText = 'This is a really long random string and there is no way it fits in 100 pixels.'; var $wrapper = createWrappedDiv( origText, '100px' ); diff --git a/tests/qunit/suites/resources/mediawiki/mediawiki.js b/tests/qunit/suites/resources/mediawiki/mediawiki.js index 9444dbdc43..3ebd9ca1d5 100644 --- a/tests/qunit/suites/resources/mediawiki/mediawiki.js +++ b/tests/qunit/suites/resources/mediawiki/mediawiki.js @@ -159,7 +159,7 @@ test( 'mw.loader', function() { // Extract path var tests_path = rePath.exec( location.href ); - mw.loader.implement( 'is.awesome', [tests_path + 'sample/awesome.js'], {}, {} ); + mw.loader.implement( 'is.awesome', [QUnit.fixurl( tests_path + 'data/defineTestCallback.js')], {}, {} ); mw.loader.using( 'is.awesome', function() { -- 2.20.1